package com.mes.web.controller.jy;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.mes.entity.jy.Regular;
import com.mes.entity.jy.RegularDetail;
import com.mes.service.jy.RegularDetailService;
import com.mes.service.jy.RegularServise;
import com.mes.utils.ResponseUtil;

/**
 * 条码规则Controller
 * @author jyy
 * @version1.0
 * 2019-02-13 14:55:40
 */
@RequestMapping("/regular")
@Controller
public class RegularController {

	@Resource
	private RegularServise regularServise;
	@Resource
	private RegularDetailService regularDetailService;
	
	/**
	 * 去产品条码规则添加页面
	 * @return 添加页面
	 */
	@RequestMapping("/toCreateregular")
	public String toCreateregular() {
		System.out.println("111");
		return "/jyy/createRegular";
	}
	
	/**
	 * 新增产品条码规则
	 * @param regular
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/createRegular")
	public void createRegular(int rowCount,Regular regular,String[] code,String[] content,String[] mean,HttpServletResponse response) throws Exception {
		//添加前先判断是否已经存在
		int affact = 0;
		List<Regular> list = regularServise.selectRegularBy(regular);
		if(list.size() > 0) {
			affact = 2;
		}else {
			affact = regularServise.createRegular(regular);
			for (int i = 0; i < rowCount; i++) {
				RegularDetail detail = new RegularDetail();
				detail.setRid(String.valueOf(regular.getId()));
				if(rowCount == 1){
					if(code.length > 0){
						detail.setCode(code[i]);
					}
					if(content.length > 0){
						detail.setContent(content[i]);
					}
					if(mean.length > 0){
						detail.setMean(mean[i]);
					}
				}else{
					detail.setCode(code[i]);
					detail.setContent(content[i]);
					detail.setMean(mean[i]);
				}
				regularDetailService.insert(detail);
			}
		}
		ResponseUtil.write(response, affact);
	}
	
	
	/**
	 * 分页查询产品条码规则
	 * @param queryjy 条件查询实体
	 * @param request 请求
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/selectRegular")
	public void selectRegular(Regular regular, HttpServletRequest request, HttpServletResponse response) throws Exception {
		int count = regularServise.countRegular(regular);
		List<Regular> list= regularServise.selectRegularPagine(regular);
		 
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", count);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 根据id查询产品条码规则
	 * @param id 产品条码规则id
	 * @param response 响应
	 * @throws Exception 异常
	 */
	@RequestMapping("/toEditCarRoom")
	public void toEditCarRoom(long id,HttpServletResponse response) throws Exception {
		Regular regular = regularServise.selectRegularByid(id);
		Gson gson = new Gson();
	    String json = gson.toJson(regular);
	    ResponseUtil.write(response, json);
	}
	
	/**
	 * 修改产品条码规则
	 * @param regular 产品条码规则实体
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/editCarRoom")
	public void editCarRoom(int rowCount,Regular regular,String[] code,String[] content,String[] mean,HttpServletResponse response) throws Exception {
		//修改前判断是否已存在
		int affact = 0;
		Regular regular1 = new Regular();
		regular1.setPid(regular.getPid());
		List<Regular> list = regularServise.selectRegularBy(regular1);
		if(list.size() > 1 || (list.size() == 1 && !regular.getId().equals(list.get(0).getId()))) {
			affact = 2;
		}else {
			affact = regularServise.editRegular(regular);
			regularDetailService.deleteByRid(String.valueOf(regular.getId()));
			for (int i = 0; i < rowCount; i++) {
				RegularDetail detail = new RegularDetail();
				detail.setRid(String.valueOf(regular.getId()));
				if(rowCount == 1){
					if(code.length > 0){
						detail.setCode(code[i]);
					}
					if(content.length > 0){
						detail.setContent(content[i]);
					}
					if(mean.length > 0){
						detail.setMean(mean[i]);
					}
				}else{
					detail.setCode(code[i]);
					detail.setContent(content[i]);
					detail.setMean(mean[i]);
				}
				regularDetailService.insert(detail);
			}
		}
		ResponseUtil.write(response, affact);
	}
	
	/**
	 * 删除产品条码规则（批量）
	 * @param ids 所有id以“，”号拼接的字符串
	 * @param response 响应
	 * @throws Exception 
	 */
	@RequestMapping("/deleteRegular")
	public void deleteRegular(String ids,HttpServletResponse response) throws Exception {
		int affact = regularServise.deleteRegular(ids);
		ResponseUtil.write(response, affact);
	}
}
